package fw;

import static fw.Config.BROWSER_NAME;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fw.helpers.ActionBuilderHelper;
import fw.helpers.AjaxHelper;
import fw.helpers.DBHelper;
import fw.helpers.ErrorsHelper;
import fw.helpers.LinkCheckHelper;
import fw.helpers.NavigationHelper;
import fw.helpers.ProxyHelper;
import fw.helpers.RandomizeHelper;
import fw.helpers.WebDriverHelper;
import fw.helpers.WebElementFinderHelper;
import fw.helpers.WebElementHelper;

public class ApplicationManager {
	@SuppressWarnings("unused")
	private WebDriver driver;
	public WebDriverWait wait;

	public String baseUrl;
	public String errorCollector = "";

	private NavigationHelper navigationHelper;
	private RandomizeHelper randomizeHelper;
	private WebDriverHelper webDriverHelper;
	private ProxyHelper proxy;
	private WebElementHelper webElement;
	private WebElementFinderHelper find;
	private String testPage;
	private Telerik telerik;
	private ErrorsHelper errors;
	private AjaxHelper js;
	private DBHelper db;
	private LinkCheckHelper linkCheck;
	private MyDvdCollection myDvdCollection;
	private ActionBuilderHelper actionBuilder;

	/* Logger */
	public static Logger LOG = LoggerFactory.getLogger(ApplicationManager.class);

	public ApplicationManager() {
	}

	public void init() {
		new Config(this);
		driver = getDriver();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		wait = new WebDriverWait(driver, 5);
		driver.manage().window().maximize();
	}

	public void stop() {
		getDriver().quit();
	}

	public WebDriver getDriver() {
		if (driver == null) {
			driver = webDriverHelper().runLocalBrowser(BROWSER_NAME);
		}
		return driver;
	}

	/*** PROJECTS ***/

	public Telerik telerik() {
		if (telerik == null) {
			telerik = new Telerik(this);
		}
		return telerik;
	}

	public MyDvdCollection myDvdCollection() {
		if (myDvdCollection == null) {
			myDvdCollection = new MyDvdCollection(this);
		}
		return myDvdCollection;
	}

	/*** HELPERS ***/

	public WebDriverHelper webDriverHelper() {
		if (webDriverHelper == null) {
			webDriverHelper = new WebDriverHelper(this);
		}
		return webDriverHelper;
	}

	public ProxyHelper proxy() {
		if (proxy == null) {
			proxy = new ProxyHelper(this);
		}
		return proxy;
	}

	public WebElementHelper webElement() {
		if (webElement == null) {
			webElement = new WebElementHelper(this);
		}
		return webElement;
	}

	public WebElementFinderHelper elementFinder() {
		if (find == null) {
			find = new WebElementFinderHelper(this);
		}
		return find;
	}

	public NavigationHelper navigate() {
		if (navigationHelper == null) {
			navigationHelper = new NavigationHelper(this);
		}
		return navigationHelper;
	}

	public RandomizeHelper randomize() {
		if (randomizeHelper == null) {
			randomizeHelper = new RandomizeHelper(this);
		}
		return randomizeHelper;
	}

	public ErrorsHelper errors() {
		if (errors == null) {
			errors = new ErrorsHelper(this);
		}
		return errors;
	}

	public AjaxHelper js() {
		if (js == null) {
			js = new AjaxHelper(this);
		}
		return js;
	}

	public DBHelper db() {
		if (db == null) {
			db = new DBHelper(this);
		}
		return db;
	}

	public LinkCheckHelper linkCheck() {
		if (linkCheck == null) {
			linkCheck = new LinkCheckHelper(this);
		}
		return linkCheck;
	}

	public ActionBuilderHelper actionBuilder() {
		if (actionBuilder == null) {
			actionBuilder = new ActionBuilderHelper(this);
		}
		return actionBuilder;
	}

	/**
	 * Set test page by name
	 * 
	 * @param p_pageName
	 */
	public void setTestPage(String p_pageName) {
		LOG.debug("Set test page: " + p_pageName);
		testPage = p_pageName;
	}

	/**
	 * Reset value of test page
	 */
	public void resetTestPage() {
		testPage = null;
	}

}
